#include <iostream>
#include <string>
#include "BattleGame.h"
#include "Language.h"
using namespace std;

#include <QApplication>
#include <QDesktopWidget>
#include <QIcon>
#include <QPixmap>
#include <QDir>
#include <QByteArray>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QInputDialog>
#include "MainWindow.h"

void getDirectory() {
	QDir custom, home = QDir::home();
	QString dir;
	bool ok;
	do {
		dir = QInputDialog::getText(NULL, QString(DIRECTORYOFINSTALLATION), QString(INSERTDIRECTORY), QLineEdit::Normal, QDir::home().path(), &ok, Qt::Dialog);
		custom.setPath(dir);
	} while (/*!ok &&*/ !custom.exists());
	QFile settings(home.filePath("/.BattleShipSettings.txt"));
	/*if (*/settings.open(QIODevice::WriteOnly  | QIODevice::Text);/*) {*/
		QTextStream out(&settings);
		out << dir << endl;
		settings.close();
// 	}
	QDir::setSearchPaths("data", QStringList(dir));
}

void center(MainWindow& window, int w, int h) {
	QDesktopWidget *desktop = QApplication::desktop();
	int screenWidth = desktop->width();
	int screenHeight = desktop->height();
	window.move((screenWidth-w)/2, (screenHeight - h)/2);
}

int main(int argc, char *argv[]) {
	QApplication app(argc, argv);
	/*QDir home = QDir::home();
	QDir temp = QDir::current();
	temp.makeAbsolute();
	QDir current(temp.path() + "/application_data");
	QFile settings(home.filePath("/.BattleShipSettings.txt"));
	if (settings.open(QIODevice::ReadOnly | QIODevice::Text)) {
		QByteArray line = settings.readLine();
		QString dir(line.constData());
		dir.append("/application_data");
		QDir custom(dir);
		if (custom.exists()) {
			QDir::setSearchPaths("data", QStringList(dir));
		}
		else {
			getDirectory();
		}
	}
	else if (current.exists()) {*/
		QDir::setSearchPaths("data", QStringList(":/application_data"));
	/*}
	else getDirectory();*/
	MainWindow window;
	QDesktopWidget *desktop = QApplication::desktop();
	window.resize(desktop->width(), desktop->height());
	center(window,desktop->width(), desktop->height());
	window.setWindowTitle(BATTLESHIP);
	window.setWindowIcon(QIcon(QPixmap("data:velenoso.gif")));
	window.show();
	return app.exec();
}
